import type { RouteLocationNormalizedGeneric } from "vue-router";
import useMenuStore from "@/pinia/menu";
import useUserStore from "@/pinia/user";

// 设置对应路由的浏览器标签标题
export const setBrowserTitle = (to: RouteLocationNormalizedGeneric) => {
  document.title = ("待办系统 - " + to.meta.title) as string;
};

export const getMenuList = async (to: RouteLocationNormalizedGeneric) => {
  if (["404", "Login", "Logout"].includes(to.path)) return;
  const menuStore = useMenuStore();
  await menuStore.getMenuList();
};

// 判定用户是否有当前跳转菜单的权限
export const hasMenuPermission = (to: RouteLocationNormalizedGeneric) => {
  const userStore = useUserStore();
  const auths = [
    ...(userStore.userInfo?.auths?.halfPermissionKey || []),
    ...(userStore.userInfo?.auths?.permissionKey || []),
  ];
  return auths.includes(to.name as string);
};
